import axios from 'axios'
import { showFailToast } from 'vant';
import router from '@/router/index.js'

axios.defaults.baseURL = 'http://121.43.166.28:7009'  // 基础域名
axios.defaults.withCredentials = true   // 允许axios携带凭证
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest'
// 将一个能证明身份的东西放在请求头中
axios.defaults.headers['Authorization'] = localStorage.getItem('token') || null
axios.defaults.headers.post['Content-Type'] = 'application/json'


// 响应拦截器(该项目中每一个请求发出去后，后端发回来的响应都会先走这里)
axios.interceptors.response.use((res => {
    // 每个接口的错误情况都统一处理
    if (typeof (res.data) !== 'object') {
        showFailToast('服务器异常，请稍后重试')
        return Promise.reject(res)
    }
    if (res.data.code !== 200) {
        if (res.data.msg) showFailToast(res.data.msg)

        if (res.data.code = 401) {
            // 跳转到登陆页
            router.push('/login')
        }
        //return Promise.reject(res)
    }
    return res.data
}))

export default axios